/**
 * 数据源对照标化字典初始化
 */
var SdsDataContrast = {
    id: "SdsDataContrastTable",	//表格id
    seItem: null,		//选中的条目
    dicId: null,
    dataId:null,
    table: null,
    ztree: null,
    standardtree:null,
    layerIndex: -1,
    parentId:0
};
SdsDataContrast.refresh = function () {
    SdsDataContrast.table.refresh();
    SdsDataContrast.ztree.init();
    SdsDataContrast.standardtree.init();
}
// Dept.refresh =function(){
//     Dept.table.refresh();
//     Dept.ztree.init();
// };
/**
 * 初始化表格的列
 */
SdsDataContrast.initColumn = function () {
    return [
        {title: '数据源', field: 'sourceDesc', align: 'center', width: '200px', valign: 'middle'},
        {title: '标化字典', field: 'name', align: 'center', width: '350px', valign: 'middle'},
        {title: '对照类型', field: 'operaType', align: 'center', width: '350px', valign: 'middle',formatter:function (value,row,index){
            if (value == 1)
                return "对照";
            if (value == 0)
                return "取消对照";
            }},
        {title: '对照时间', field: 'operaTime', align: 'center',width: '350px', valign: 'middle', sortable: true},
        {title: '操作人', field: 'operaName', align: 'center', width: '200px', valign: 'middle'}
    ];
};


/**
 * 字典树点击事件
 */
SdsDataContrast.onClickStandTree = function (e, treeId, treeNode){
    if((!(SdsDataContrast.dataId))||SdsDataContrast.dataId==0){
        Sys.info("请先选择要对照的数据源！")
        return;
    }
    var _data = {
        dataId:SdsDataContrast.dataId,
        dicId:treeNode.id,
        isAdd:treeNode.checked
    };
    // 对照
    Sys.ajax(Sys.backendPath + "/sds/sdsstanddatacontrast/contrast",_data, function (res) {
        if(res.data==true){
            Sys.info("对照成功!");
        }
        else {
            Sys.info("取消对照成功！");
        }

    });
};

/**
 *  点击数据源切换对于对照日志
 */
SdsDataContrast.reselect = function (){
    var queryData = {};
    queryData['dataId'] = SdsDataContrast.dataId;
    queryData['sourceDesc'] = null;
    queryData['name'] = null;
    SdsDataContrast.table.refresh({query: queryData});
};

/**
 * 数据树点击事件
 */
SdsDataContrast.onClickData = function (e, treeId, treeNode) {
    SdsDataContrast.standardtree.getObject().checkAllNodes(false);
    SdsDataContrast.dataId = treeNode.id;
    // 加载对照关系
    Sys.ajax(Sys.backendPath + "/sds/sdsstanddatacontrast/loadContrastByDataId",{dataId:treeNode.id}, function (res) {
        var dicIds = res.data.dic;
        if(dicIds){
            for (var i = 0; i < dicIds.length; i++) {
                var node = SdsDataContrast.standardtree.getObject().getNodeByParam("id", dicIds[i], null);
                if(node){
                    SdsDataContrast.standardtree.getObject().checkNode(node, true, false);
                }
            }
        }
    });
    SdsDataContrast.dataId = treeNode.id;
    SdsDataContrast.reselect();
};



/**
 * 查询列表
 */
SdsDataContrast.search = function () {
    var queryData = {};
    queryData['dataId'] = '';
    queryData['sourceDesc'] = $("#datacondition").val();
    queryData['name'] = $("#sdscondition").val();
    SdsDataContrast.table.refresh({query: queryData});
};






$(function () {

    $HUI.tabs("#tt2",{
        onSelect:function(title){
            $.messager.popover({type:'info',msg:'切换到【'+title+'】'});
            SdsDataContrast.table.refresh();
        }
    });

    var defaultColunms = SdsDataContrast.initColumn();
    var table = new BSTable(SdsDataContrast.id, Sys.backendPath + "/sds/sdsstanddatacontrast/contrastLog", defaultColunms);
    table.setPaginationType("server");
    table.setSubHeight(-10);
    table.setSort("operaTime","DESC");
    table.setPageSize(10);
    table.init();
    SdsDataContrast.table = table;

    var ztree = new $ZTree("datasourceTree", Sys.backendPath + "/sds/sdsstanddatacontrast/datatree");
    ztree.bindOnClick(SdsDataContrast.onClickData);
    ztree.init();
    SdsDataContrast.ztree = ztree;
    fuzzySearch('datasourceTree','#seachKey',null,true);

    var standardTreeSetting = {
        check: {
            enable: true,
            chkboxType: {
                "Y": "",
                "N": ""
            }
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
             onCheck: SdsDataContrast.onClickStandTree
        }
    };


    var standardtree = new $ZTree("standardTree", Sys.backendPath + "/sds/sdsstanddatacontrast/standardtree");
    standardtree.setSettings(standardTreeSetting);
    standardtree.init();
    SdsDataContrast.standardtree = standardtree;
    fuzzySearch('standardTree','#seachKey',null,true);
});
